package club.menglinking.wechat.controller.wechatController;

import club.menglinking.wechat.entity.AesException;
import club.menglinking.wechat.entity.TextTypeEntity;
import club.menglinking.wechat.entity.enumbeen.NotFonudMsgBack;
import club.menglinking.wechat.entity.enumbeen.Req_Msg_Type;
import club.menglinking.wechat.service.wechatservice.TextServiceType;
import club.menglinking.wechat.util.BaseUtil;
import club.menglinking.wechat.util.WeChatUtil;
import club.menglinking.wechat.util.XmlUtil;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

/**
 * @ClassName ConnectWechat
 * @Author Administrator
 * @Date 2018/12/30 13:14
 * @Version 1.0
 * @Description
 **/
@Controller
public class ConnectWechat {

    @Value("${token}")
    private String token;

    @Autowired
    @Qualifier("textServiceType")
    private TextServiceType textServiceType;


    @GetMapping("/connection")
    @ResponseBody
    public String Connection(@RequestParam String signature,@RequestParam String timestamp,
                             @RequestParam String nonce,@RequestParam String echostr){
        /**
         *@MethodName Connection
         *@Data 2018/12/30 13:15
         *@Author wuxuan
         *@Description 接受微信服务器验证的请求接口
         **/
        System.out.println("开始验证-------微信接入服务");
        String sha1=null;
        try {
             sha1= WeChatUtil.getWeChatUtil().getSHA1(token, timestamp, nonce, "");
        } catch (AesException e) {
            e.printStackTrace();
        }
        System.out.println(signature);
        System.out.println(sha1);
        if (sha1.equals(signature)){
            System.out.println("验证成功------验证结束");
            return echostr;
        }else {
            System.out.println("未知请求------验证结束");
            return null;
        }

    }
    @PostMapping("/connection")
    @ResponseBody
    public String HandleMsg(HttpServletRequest request, HttpServletResponse response) throws IOException, DocumentException {
        String xmlMsg=null;
        /**
         *@MethodName HandleMsg
         *@Data 2018/12/30 14:51
         *@Author wuxuan
         *@Description 处理微信服务器的服务
         **/
        Map<String, String> xml = XmlUtil.getXmlUtil().xmlToMap(request);
        String msgType = xml.get("MsgType");
        if (msgType.equals(Req_Msg_Type.text.toString())){
            xmlMsg=textServiceType.handlealltext(xml);
        }else if(msgType.equals(Req_Msg_Type.image.toString())){

        }else if (msgType.equals(Req_Msg_Type.voice.toString())){

        }else if (msgType.equals(Req_Msg_Type.video.toString())){

        }else if (msgType.equals(Req_Msg_Type.event.toString())){
            String msg = xml.get("Event");
            switch (msg) {
                case "subscribe"://关注事件
                    xmlMsg = textServiceType.handlealltext(xml);
                    break;
                case "CLICK"://点击事件
                    xmlMsg = textServiceType.handlealltext(xml);
                    break;
                case "VIEW": //跳转页面事件
                    System.out.println(xml.toString());
                    break;
                default:
                    xml.put("EventKey","找不到");
                    xmlMsg = textServiceType.handlealltext(xml);
            }
        }else if (msgType.equals(Req_Msg_Type.link.toString())){

        }else if (msgType.equals(Req_Msg_Type.location.toString())){

        }else {
            System.out.println("没有匹配项");
            //获取整形的时间
            String newTime= BaseUtil.getBaseUtil().getIntegerTime();
            String ToUserName=xml.get("ToUserName");
            String FromUserName=xml.get("FromUserName");
            System.out.println(msgType);
            TextTypeEntity Tentity=new TextTypeEntity();
            Tentity.setToUserName(FromUserName);
            Tentity.setFromUserName(ToUserName);
            Tentity.setCreatetime(newTime);
            Tentity.setMsgType("text");
            Tentity.setContent(NotFonudMsgBack.getMsg(4));
            xmlMsg = XmlUtil.getXmlUtil().ReturnMsg(Tentity);
        }
        return xmlMsg;
    }

}
